package com.dayatang.android.map;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.dayatang.android.Activity.R;
import com.dayatang.android.Activity.SubprojectMapActivity;
import com.dayatang.android.Model.Direction;
import com.dayatang.android.config.LocationConfig;
import com.dayatang.android.config.NavigationConfig;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

public class SubOverItem extends ItemizedOverlay<OverlayItem> {

	private Drawable marker;
	private SubprojectMapActivity context;
	
	private TextView subProjName;
	private TextView subProjProgress;
	private TextView subProjBudget;
	private TextView subProjOutput;
	
	private String endLocation;
	private List<OverlayItem> geoList = new ArrayList<OverlayItem>();
	
	public SubOverItem(Drawable defaultMarker,Context context,List<GeoPoint> geoPoints,List<String> snippet) {
		super(boundCenterBottom(defaultMarker));
		this.marker = defaultMarker;
		this.context = (SubprojectMapActivity) context;
		InitGeoPoints(geoPoints,snippet);
		populate();
	}

	private void InitGeoPoints(List<GeoPoint> geoPoints,List<String> snippet) {
		int i = 0;
		for (GeoPoint geoPoint : geoPoints) {
			geoList.add(new OverlayItem(geoPoint, "", snippet.get(i)));
			i++;
		}
	}
	
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		Projection projection = mapView.getProjection();
		for (int index = size() - 1; index >= 0; index--) {
			OverlayItem overLayItem = getItem(index);
			String title = overLayItem.getTitle();
			Point point = projection.toPixels(overLayItem.getPoint(), null); 
			
			Paint paintCircle = new Paint();
			paintCircle.setColor(Color.RED);
			canvas.drawCircle(point.x, point.y, 5, paintCircle); // 画圆

			Paint paintText = new Paint();
			paintText.setColor(Color.BLACK);
			paintText.setTextSize(15);
			canvas.drawText(title, point.x, point.y - 25, paintText); // 绘制文本
		}
		
		super.draw(canvas, mapView, shadow);
		boundCenterBottom(marker);
	}
	
	protected boolean onTap(int index) {
		setFocus(geoList.get(index));
		
		MapView.LayoutParams geoLP = (MapView.LayoutParams) context.popView.getLayoutParams();
		geoLP.point = geoList.get(index).getPoint();
		context.mapView.getController().animateTo(geoLP.point);
		context.mapView.updateViewLayout(context.popView, geoLP);
		context.popView.setVisibility(View.VISIBLE);
		endLocation = geoList.get(index).getSnippet();
		InitView(geoList.get(index).getSnippet());
		
		return true;
	}
	
	private void InitView(String jsonData) {
		subProjName = (TextView) context.findViewById(R.id.map_subproject_name);
		subProjProgress = (TextView) context.findViewById(R.id.map_subproject_progress);
		subProjBudget = (TextView) context.findViewById(R.id.map_subproject_budget);
		subProjOutput = (TextView) context.findViewById(R.id.map_subproject_finish_output);
		
		try {
			JSONObject jsonObject = new JSONObject(jsonData);
			subProjName.setText(jsonObject.getString("subProjName"));
			subProjProgress.setText(jsonObject.getString("subProjProgress"));
			subProjBudget.setText(jsonObject.getString("subProjBudget"));
			subProjOutput.setText(jsonObject.getString("subProjOutput"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		Button navigationBtu = (Button) context.findViewById(R.id.map_subproject_navigation);
		navigationBtu.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v) {
				Log.i("123", "aa");
				String url = "http://maps.google.com/maps/api/directions/json?origin=" + getStartLocation() 
						+ "&destination=" + getEndLocation(endLocation)
						+ "&mode=driving"
						+ "&sensor=false" 
						+ "&language=zh-CN";	
				Log.i("123", "bb");
				Direction direction = NavigationConfig.getNavigationDirection(context, url);
				List<GeoPoint> geoPoints = Kit.decodePoly(direction.getRoutes().get(0).getOverview_polyline().getPoints());
				Log.i("123", "cc");		
				Drawable marker = context.getResources().getDrawable(R.drawable.map_overlay); 
				marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());   //为maker定义位置和边界
				if (geoPoints != null) {
					context.mapView.getController().setZoom(18);
					context.mapView.getController().animateTo(geoPoints.get(0));
					context.popView.setVisibility(View.VISIBLE);
					context.mapView.getOverlays().add(new PolyLineLayer(geoPoints)); 
					context.overlays.add(new StepOverItem(marker, context, direction.getRoutes().get(0).getLegs()));
				} else {
					Toast.makeText(context, "导航失败", Toast.LENGTH_LONG).show();
				}
			}
		});
		
	}
	
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		context.popView.setVisibility(View.GONE);
		return super.onTouchEvent(event, mapView);
	}
	
	protected OverlayItem createItem(int i) {
		return geoList.get(i);
	}

	public int size() {
		return geoList.size();
	}
	
	private String getEndLocation(String data) {
		try {
			JSONObject jsonObject = new JSONObject(data);
			return jsonObject.getString("latitude") +","+ jsonObject.getString("longitude");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private String getStartLocation() {
		try {
			double[] ds = LocationConfig.getLongitudeAndLatitude(context);
			return String.valueOf(ds[1]) +","+ String.valueOf(ds[0]);
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
